<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function fb_user_from_memcache($user_id, $result = NULL) {
    $key = "fbu_" . $user_id;
    static $memcache = FALSE;
    if (!$memcache) {
        global $GS_CONFIG;
        $host = $GS_CONFIG->memcache;
        $port = 11211;
        $memcache = new Memcache();
        $memcache->addServer($host, $port);
        $stats = @$memcache->getExtendedStats();
        $available = (bool) $stats["$host:$port"];
        if (!( $available && @$memcache->connect($host, $port))) {
            return $result;
        }
    }
    if (empty($result)) {
        $cached_result = $memcache->get($key);
        return $cached_result;
    } else {
        $memcache->set($key, $result, false, 7200);
        return $result;
    }
}

function dt_get_users_by_ids($ids) {
    if (!$ids || !is_array($ids)) {
        return NULL;
    }
    $list = "(-1";
    foreach ($ids as $id) {
        $id = mysql_real_escape_string($id);
        $list .= ", '$id'";
    }
    $list .= ")";

    $query = "SELECT user_id, facebook_id, username, avatar_url, fullname FROM draw_together_fb_users WHERE user_id IN $list";
    $result = safe_select_query($query);
    return $result;
}

function dt_get_users_by_fb_ids($facebook_ids) {
    if (!$facebook_ids || !is_array($facebook_ids)) {
        return NULL;
    }
    $list = "(-1";
    foreach ($facebook_ids as $facebook_id) {        
        $facebook_id = floatval(mysql_real_escape_string($facebook_id));
        if ($facebook_id > 0) {
            $list .= ", '$facebook_id'";
        }
    }
    $list .= ")";

    $query = "SELECT user_id, facebook_id, username, avatar_url, fullname FROM draw_together_fb_users WHERE facebook_id IN $list";
    $result = safe_select_query($query);
    return $result;
}

function dt_get_random_users_by_fb_ids($facebook_ids) {
    if (!$facebook_ids || !is_array($facebook_ids)) {
        return NULL;
    }
    $list = "(-1";
    foreach ($facebook_ids as $facebook_id) {
        $facebook_id = mysql_real_escape_string($facebook_id);
        $list .= ", '$facebook_id'";
    }
    $list .= ")";

    $user_id = get_loggedin_userid();
    $old_list = "($user_id";
    $games = dt_list_new_game($user_id);
    foreach ($games as $game) {
        if ($game->adjective_id == $user_id) {
            $old_list .= ", " . $game->noun_id;
        } else {
            $old_list .= ", " . $game->adjective_id;
        }
    }
    $old_list .= ")";
    $query = "SELECT user_id, facebook_id, username, avatar_url, fullname 
                FROM draw_together_fb_users 
                WHERE facebook_id NOT IN $list
                    AND user_id IN (SELECT user_id FROM draw_together_random_users)
                    AND user_id NOT IN $old_list
                ORDER BY RAND()
                LIMIT 0, 1";
    $result = safe_select_query($query);
    return $result[0];
}

function dt_get_user_by_fb_email($email) {
    $email = mysql_real_escape_string($email);
    $query = "SELECT user_id, facebook_id, username, avatar_url, fullname FROM draw_together_fb_users WHERE email = '$email'";
    $result = safe_select_query($query);
    return $result[0];
}

function dt_get_user_by_fb_id($facebook_id) {
    $facebook_id = mysql_real_escape_string($facebook_id);
    $query = "SELECT user_id, facebook_id, username, avatar_url, fullname FROM draw_together_fb_users WHERE facebook_id = '$facebook_id'";
    $result = safe_select_query($query);
    return $result[0];
}

function dt_get_user_by_user_id($user_id) {
    $result = fb_user_from_memcache($user_id);
    if (empty($result)) {
        $user_id = mysql_real_escape_string($user_id);
        $query = "SELECT user_id, facebook_id, username, avatar_url, fullname FROM draw_together_fb_users WHERE user_id = '$user_id'";
        $result = safe_select_query($query);
        fb_user_from_memcache($user_id, $result);
        return $result[0];
    }
    return $result[0];
}

function dt_add_fb_user($user_id, $facebook_id, $username, $avatar_url, $fullname, $pro) {
    $facebook_id = mysql_real_escape_string($facebook_id);
    $user_id = mysql_real_escape_string($user_id);
    $username = mysql_real_escape_string($username);
    $avatar_url = mysql_real_escape_string($avatar_url);
    $fullname = mysql_real_escape_string($fullname);
    $timestamp = time();
    if ($user_id <= 0) {
        return FALSE;
    }
    global $CONFIG;
    $query = "INSERT INTO draw_together_fb_users(user_id, facebook_id, username, avatar_url, fullname, timestamp)
                VALUES('$user_id', '$facebook_id', '$username', '$avatar_url', '$fullname', '$timestamp')";
    $query2 = "UPDATE {$CONFIG->dbprefix}users_entity SET facebook_id=$facebook_id WHERE guid=$user_id";

    if (safe_insert_query($query) && safe_update_query($query2)) {
        //dt_update_coins($user_id, $pro ? 299 : 99);
        return TRUE;
    }
    return FALSE;
}

function dt_register_random_user($user_id) {
    $user_id = mysql_real_escape_string($user_id);
    $timestamp = time();
    if ($user_id <= 0) {
        return FALSE;
    }
    $query = "INSERT INTO draw_together_random_users(user_id, timestamp)
                VALUES('$user_id', '$timestamp')";
    if (safe_insert_query($query)) {
        return TRUE;
    }
    return FALSE;
}

?>
